<?php
namespace Common;
class FileUtil{
    
    public static function readThriftServer($file = null){
        if(empty($file) == true){
            $file = dirname(__FILE__) . '/../../data/thrift_server.data';
        }
        $content = FileUtil::readFile($file);
        return $content;
    }
    public static function saveThriftServer($data, $file = null){
        if(empty($file) == true){
            $file = dirname(__FILE__) . '/../../data/thrift_server.data';
        }
        FileUtil::writeFile($file, $data);
    }
    
    public static function  writeFile($filename, $str) 
    {
        if (function_exists('file_put_contents'))
        {
            file_put_contents($filename, $str);
        }
        else
        {
            $fp = fopen($filename, "wb");
            fwrite($fp, $str);
            fclose($fp);
        }
    }
    
    /**
     * 获取指定目录中所有文件
     * @param string $directory
     * @param array $files
     * @return array
     */
    public static function getPathFiles ($directory = null, $files = array()) {

        $iterator = new \DirectoryIterator ( $directory );
        foreach ( $iterator as $info ) {
            if ($info->isFile ()) {
                $files [] = $directory . DIRECTORY_SEPARATOR . $info->getFilename();
            } elseif (!$info->isDot ()) {
                $list = self::getPathFiles(
                    $directory.DIRECTORY_SEPARATOR.$info->__toString ()
                );
                if(!empty($files))
                       $files = array_merge($files, $list);
                else {
                    $files = $list;
                }
            }
        }
        return $files;
    }
    
    
    public static function readFile($filename) 
    {
        if (function_exists('file_get_contents'))
        {
            return file_get_contents($filename);
        }
        else
        {
            $fp = fopen($filename, "rb");
            $str = fread($fp, filesize($filename));
            fclose($fp);
            return $str;
        }
    }
}

?>